package cn.jiaqiang.chatgpt.data.config;


import com.jiaqiang.chatgpt.session.*;
import com.jiaqiang.chatgpt.session.defaults.DefaultOpenAiSessionFactory;
import com.jiaqiang.chatgpt.session.defaults.DefaultUserSessionOpenAiSessionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * OpenAiSession 工厂配置开启
 */
@Configuration
@EnableConfigurationProperties(ChatGPTSDKConfigProperties.class)
public class ChatGPTSDKConfig {

    @Value("${proxy.host:39.98.44.8}")
    private String host;


    @Value("${proxy.port:7890}")
    private Integer port;

    /**
     * 对话相关 openSession
     * @param properties 配置 bean
     */
    @Bean
    public OpenAiSession openAiSession(ChatGPTSDKConfigProperties properties) throws Exception {
        // 1. 配置文件
        GptConfiguration gptConfiguration = new GptConfiguration();
        gptConfiguration.setHost(host);
        gptConfiguration.setPort(port);
        gptConfiguration.setApiHost(properties.getApiHost());
        gptConfiguration.setApiKey(properties.getApiKey());
        gptConfiguration.setAuthToken(properties.getAuthToken());
        // 2. 会话工厂
        OpenAiSessionFactory factory = new DefaultOpenAiSessionFactory(gptConfiguration);
        // 3. 开启会话
        return factory.openSession();
    }

    /**
     * 用户(我)相关 openSession
     * @param properties 配置 bean
     */
    @Bean
    public UserOpenAiSession userOpenAiSession(ChatGPTSDKConfigProperties properties) {
        // 1. 配置文件
        GptConfiguration gptConfiguration = new GptConfiguration();
        gptConfiguration.setHost(host);
        gptConfiguration.setPort(port);
        gptConfiguration.setApiHost(properties.getApiHost());
        gptConfiguration.setApiKey(properties.getApiKey());
        gptConfiguration.setAuthToken(properties.getAuthToken());
        gptConfiguration.setSessionKey(properties.getSessionKey());
        // 2. 会话工厂
        DefaultUserSessionOpenAiSessionFactory factory = new DefaultUserSessionOpenAiSessionFactory(gptConfiguration);
        // 3. 开启会话
        return factory.openSession();
    }

}
